{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "24fa96ee",
   "metadata": {},
   "source": [
    "# Program structure."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8374b0c",
   "metadata": {},
   "source": [
    "计算机流程控制\n",
    "顺序结构\n",
    "选择结构 if语句\n",
    "循环结构 while语句，for-in语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6b48c25",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------程序开始-----\n",
      "1.冰箱门打开\n",
      "2.大象放冰箱里\n",
      "3.冰箱门关上\n",
      "------程序结束-----\n"
     ]
    }
   ],
   "source": [
    "# 把大象装冰箱分几步\n",
    "print(\"------程序开始-----\")\n",
    "print(\"1.冰箱门打开\")\n",
    "print(\"2.大象放冰箱里\")\n",
    "print(\"3.冰箱门关上\")\n",
    "print(\"------程序结束-----\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39b9070e",
   "metadata": {},
   "source": [
    "# 对象的布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35c2d15c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#所有对象都有一个布尔值\n",
    "#获取对象布尔值\n",
    "#使用内置函数bool()\n",
    "\n",
    "#以下对象的布尔值为False\n",
    "#Flase，\n",
    "#数值()\n",
    "#None\n",
    "#空字符串\n",
    "#空列表\n",
    "#空元组\n",
    "#空字典\n",
    "#空集合\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9df0956e",
   "metadata": {},
   "source": [
    "## 测试对象的布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "107f5ce8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(bool(False))\n",
    "print(bool(0))\n",
    "print(bool(0.0))\n",
    "print(bool(None))\n",
    "print(bool(''))\n",
    "print(bool(\"\"))\n",
    "print(bool([]))#空列表\n",
    "print(bool(list()))#空列表\n",
    "print(bool())#空元组\n",
    "print(bool(tuple()))#空元组\n",
    "print(bool({}))#空字典\n",
    "print(bool(dict()))#空字典\n",
    "print(bool(set()))#空集合\n",
    "#以上对象布尔值都是False\n",
    "#其它对象的布尔值都为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e24add20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(18))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5b6a7c88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8093c75e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(\"hello world\"))#不是空字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46ed9029",
   "metadata": {},
   "source": [
    "# 选择结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99253f32",
   "metadata": {},
   "source": [
    "## 单分支结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "93bc07b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入取款金额12\n",
      "取款成功，余额为: 988\n"
     ]
    }
   ],
   "source": [
    "money=1000 #余额\n",
    "s=int(input(\"请输入取款金额\"))#取款金额\n",
    "#进行判断是否余额充足\n",
    "if money>=s:\n",
    "    money=money-s\n",
    "    print(\"取款成功，余额为:\",money)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2750a72",
   "metadata": {},
   "source": [
    "## 双分支结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9066c507",
   "metadata": {},
   "source": [
    "如果。。不满足。。就。。\n",
    "如果是周末就不上班，不是就上班\n",
    "如果中奖就领奖，没中奖就不领"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "cd1fd057",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数:23\n",
      "23 不是整数\n"
     ]
    }
   ],
   "source": [
    "# if 条件表达式：\n",
    "    #条件执行体\n",
    "    #else \n",
    "    #条件执行体2\n",
    "#二选一之行\n",
    "#从键盘输入一个整数，编写程序让计算机判断是奇数还是偶数\n",
    "a=int(input(\"请输入一个整数:\"))\n",
    "\n",
    "#条件判断\n",
    "if a%2==0:# %求余数的意思，a除2余数为0的数字\n",
    "    print(a,\"整数\")\n",
    "else:\n",
    "    print(a,\"不是整数\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28762df4",
   "metadata": {},
   "source": [
    "## 多分支结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8329f638",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数字：70\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "#成绩在90分以上吗？不是\n",
    "#80？不是\n",
    "#70？不是\n",
    "#60？是\n",
    "\n",
    "\n",
    "a=int(input(\"请输入一个数字：\"))\n",
    "if a>=90 and a<=100:\n",
    "    print(\"A\")\n",
    "elif a>=80 and a<=70:\n",
    "    print(\"B\")\n",
    "elif a>=70 and a<=79:\n",
    "    print(\"C\")\n",
    "elif a>=60 and a<=69:\n",
    "    print(\"D\")\n",
    "else:\n",
    "    print(\"F\")\n",
    "#写python一定要记住对好空格，因为python不像c语言那样。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d1a6065",
   "metadata": {},
   "source": [
    "## if 중첩문"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a9cc3cb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#条件表达式1 为 True的时候就执行第二个表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d10b1e3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are you vip?n\n",
      "No Vip\n"
     ]
    }
   ],
   "source": [
    "answer= input(\"Are you vip?\")\n",
    "#判断是否是会员\n",
    "\n",
    "if answer =='y': #会员 vip\n",
    "    print(\"vip\")\n",
    "else:\n",
    "    print(\"No Vip\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a7976a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are you vip?n\n",
      "请输入您的购物金额:69\n",
      "全款: 69.0\n"
     ]
    }
   ],
   "source": [
    "answer= input(\"Are you vip?\")\n",
    "#判断是否是会员\n",
    "money=float(input(\"请输入您的购物金额:\"))\n",
    "\n",
    "if answer =='y': #会员 vip\n",
    "    if money >= 200:\n",
    "        print(\"付款金额为:\",0.8*money)\n",
    "    elif money >=100:\n",
    "        print(\"付款金额为:\",0.8*money)\n",
    "    else:\n",
    "        print(\"不打折，付款金额为:\",money)\n",
    "else: #非会员\n",
    "    if money >=200:\n",
    "        print(\"打9.5折:\",money * 0.9)\n",
    "    else:\n",
    "        print(\"全款:\",money)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18ab810d",
   "metadata": {},
   "source": [
    "## 조건 표현식"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9a16b909",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从键盘输入两个整数，比较两个整数大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f7d7513b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个整数10\n",
      "请输入第二个整数:3333\n",
      "10<=3333\n"
     ]
    }
   ],
   "source": [
    "num_a=int(input(\"请输入第一个整数\"))\n",
    "num_b=int(input(\"请输入第二个整数:\"))\n",
    "\n",
    "#比较大小\n",
    "\n",
    "'''\n",
    "if num_a>=num_b:\n",
    "    print(num_a,\">=\",num_b)\n",
    "else:\n",
    "    print(num_a,\"<=\",num_b)\n",
    "   ''' \n",
    "# 使用条件表达式进行\n",
    "print(str(num_a)+\">=\"+str(num_b) if num_a>=num_b else str(num_a)+\"<=\"+str(num_b))\n",
    "#这样直接写 + 号连接是不可以的，因为num_a,num_b都是整数类型\n",
    "#所以要加一个 str 连接\n",
    "# if num_a>=num_b is if，True - 执行左侧，False - 右侧"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "001d0b94",
   "metadata": {},
   "source": [
    "## Pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "69615b57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are you a member?n\n"
     ]
    }
   ],
   "source": [
    "#什么都不做，知识一个占位符，用到需要写语句的地方\n",
    "\n",
    "answer=input(\"Are you a member?\")\n",
    "\n",
    "#判断是否为会员\n",
    "if answer == 'y':\n",
    "    pass\n",
    "else:\n",
    "    pass\n",
    "#这样写的话，先占用一个语法位置不会让它报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "76a7b7d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter your age.1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "#Exp\n",
    "age = int(input(\"Please enter your age.\"))\n",
    "\n",
    "if age:\n",
    "    print(age)\n",
    "else:\n",
    "    print(\"年龄为:\",age)\n",
    "#Why does the else part get executed when inputting 0?\n",
    "# Because 0 belongs to False in the Boolean type, so the else part will be executed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e54d6626",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "269.796875px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
